#include "gui_button.h"
#include <iostream>
using namespace std;

gui_button::gui_button(int guid, char* texfile, int _x, int _y, int width, int height)
: x(_x)
, y(_y)
, state(GUI_NORMAL)
, sprite(NULL)
, down(false)
, lock(false)
{
	id = guid;
	rect.Set(x, y, x+width, y+height);
	HTEXTURE tex = hge->Texture_Load(texfile);
	HTEXTURE locktex = hge->Texture_Load("media/lock.png");
    sprite = new hgeSprite(tex, 0, 0, width, height);
	lock_sprite = new hgeSprite(locktex, 0, 0, width, height);
	bVisible = true;
	bEnabled = true;
}

gui_button::~gui_button()
{

}

void gui_button::Render()
{
    sprite->Render(x, y);
	if (lock)
	{
		lock_sprite->Render(x, y);
	}
}

void gui_button::SetLock(bool b)
{
	if (lock != b)
	{
		lock = b;
		if (b)
		{
			sprite->SetColor(ARGB(250,100,100,100));
		}
		else
		{
			sprite->SetColor(ARGB(255,255,255,255));
		}
	}
}

bool gui_button::GetIsLock()
{
    return lock;
}

void gui_button::MouseOver(bool bOver)
{
	if (lock)
	{
		return;
	}

	if (bOver)
	{
        sprite->SetColor(ARGB(255,0,255,200));
		state = GUI_MOUSE_MOVE;
	}
	else
	{
		sprite->SetColor(ARGB(255,255,255,255));
		state = GUI_MOUSE_LEAVE;
	}
}

bool gui_button::MouseLButton(bool bDown)
{
	if (lock)
	{
		return true;
	}

	if (bDown)
	{
		sprite->SetColor(ARGB(250,0,200,150));
		state = GUI_BUTTON_DOWN;
		down = true;
	}
	else
	{
		if (down)
		{
			sprite->SetColor(ARGB(255,255,255,255));
			state = GUI_BUTTON_UP;
		}
        down = false;
	}
	return true;
}

GUI_STATE gui_button::GetState()
{
    return state;
}

void gui_button::SetState(GUI_STATE s)
{
    state = s;
}
